package com.opdar.athena.client.agent.server.ajax;

import com.opdar.athena.base.Constants;
import com.opdar.athena.base.MessageType;
import com.opdar.athena.client.agent.base.Configuration;
import com.opdar.athena.client.agent.server.IoSession;
import com.opdar.athena.base.Message;
import com.opdar.athena.entities.ErrorResult;
import com.opdar.athena.messages.UserMessage;
import com.opdar.athena.utils.MessageUtil;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.*;
import io.netty.handler.codec.http.cookie.*;
import io.netty.handler.codec.http.cookie.Cookie;
import io.netty.handler.codec.http.multipart.Attribute;
import io.netty.handler.codec.http.multipart.HttpPostRequestDecoder;
import io.netty.handler.codec.http.multipart.InterfaceHttpData;

import java.util.Set;

/**
 * Created by shiju on 2017/6/27.
 */
public class AjaxHandler extends SimpleChannelInboundHandler<FullHttpRequest> {
    @Override
    public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
        super.handlerAdded(ctx);
        IoSession ioSession = Configuration.getConfiguration().getSession(ctx);
        if(ioSession == null){
            ioSession = new IoSession(ctx);
            Configuration.getConfiguration().setSession(ctx,ioSession);
        }
    }

    @Override
    public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
        super.handlerRemoved(ctx);
        IoSession ioSession = Configuration.getConfiguration().getSession(ctx);
        if(ioSession != null){
            ioSession.downline();
        }
    }

    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, FullHttpRequest request) throws Exception {
        if (HttpMethod.POST == request.method()) {
            IoSession ioSession = Configuration.getConfiguration().getSession(channelHandlerContext);
            ioSession.setHttp(true);
            // 是POST请求
            HttpPostRequestDecoder decoder = new HttpPostRequestDecoder(request);
            InterfaceHttpData type = decoder.getBodyHttpData("type");
            InterfaceHttpData tokenData = decoder.getBodyHttpData("token");
            String token = ((Attribute) tokenData).getValue();
            if (type != null && token !=null) {
                InterfaceHttpData body = decoder.getBodyHttpData("body");
                String _body = ((Attribute) body).getValue();
                Message message = MessageUtil.newMessageTypeInstance(Integer.valueOf(((Attribute) type).getValue()), _body.getBytes());
                if(message instanceof UserMessage)((UserMessage) message).setToken(token);
                IoSession session = Configuration.getConfiguration().getSession(channelHandlerContext);
                try {
                    Object result = Configuration.getConfiguration().invokeMessageTypeAction(message, session);
                    if(result != null){
                        session.write(Integer.valueOf(((Attribute) type).getValue()),result);
                    }
                } catch (Throwable throwable) {
                    throwable.printStackTrace();
                }
            }
        }
    }
}
